#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=110;
int n,k,m;
int a[N],b[N],c[N],d[N],lp[N],rp[N];
struct edge{
    int to,fl,nxt;
}g[N*N*2+N*4];
int cnt,head[N*2];
void add(int u,int v,int f){
    g[++cnt]=(edge){v,f,head[u]};
    head[u]=cnt;
    g[++cnt]=(edge){u,0,head[v]};
    head[v]=cnt;
}
int s,t;
int dep[N*2],cur[N*2];
bool bfs(){
    for(int i=1;i<=t;i++)dep[i]=0,cur[i]=head[i];
    queue<int> q;
    q.push(s);
    dep[s]=1;
    while(!q.empty()){
        int u=q.front();
        q.pop();
        for(int i=head[u];i;i=g[i].nxt){
            int v=g[i].to,f=g[i].fl;
            if(f&&!dep[v]){
                q.push(v);
                dep[v]=dep[u]+1;
            }
        }
    }
    return dep[t];
}
int dfs(int u,int lim){
    if(u==t)return lim;
    int flow=0;
    for(int &i=cur[u];i;i=g[i].nxt){
        int v=g[i].to,f=g[i].fl;
        if(!f||dep[u]+1!=dep[v])continue;
        int d=dfs(v,min(lim,f));
        flow+=d,lim-=d;
        g[i].fl-=d,g[i^1].fl+=d;
        if(!lim)return flow;
    }
    dep[u]=0;
    return flow;
}
bool check(){
    cnt=1;
    for(int i=1;i<=t;i++)head[i]=0;
    int sum=0;
    for(int i=1;i<=k;i++)add(s,i,b[i]),sum+=b[i];
    for(int i=1;i<=m;i++)add(i+k,t,c[i]);
    for(int i=1;i<=k;i++)
        for(int j=lp[i];j<=rp[i];j++)
            add(i,j+k,1);
    int res=0;
    while(bfs())res+=dfs(s,n);
    return res==sum;
}
ll ans=1e18;
void fun(int i,int lstl,int lstr,ll now){
    if(now>=ans)return;
    if(i>k){
        ans=now;
        return;
    }
    for(int r=lstr;r<=m;r++){
        lp[i]=lstl,rp[i]=r;
        if(check())fun(i+1,lstl,r,now+d[r]-d[lstl]);
    }
    for(int l=lstl+1;l<=m;l++){
        for(int r=l;r<=m;r++){
            lp[i]=l,rp[i]=r;
            if(check())fun(i+1,l,r,now+d[r]-d[l]);
        }
    }
    lp[i]=1,rp[i]=m;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    freopen("diyiti.in","r",stdin);
    freopen("diyiti.out","w",stdout);
    cin>>n>>k;
    for(int i=1;i<=n;i++)cin>>a[i];
    sort(a+1,a+n+1);
    for(int i=1;i<=n;i++){
        if(i==1||a[i]!=a[i-1])d[++m]=a[i];
        c[m]++;
    }
    for(int i=1;i<=k;i++)b[i]=n/k,lp[i]=1,rp[i]=m;
    s=m+k+1,t=m+k+2;
    if(!check()){
        cout<<-1;
        return 0;
    }
    fun(1,1,1,0);
    cout<<ans;
    return 0;
}
